<?php

class user extends main {

    //渠道
    const QD_GENUINE  = '1001000101';

    function __construct(){
        parent::__construct();
        $this->loadModel('g5_admin_user');
        $this->loadModel('g5_game');
        $this->loadModel('g5_admin_group');
        //$this->loadModel('g5_unions');
        //$this->loadModel('channels');
    }

    function index(){
        $this->userslist = $this->g5_admin_user->userlist();
        $this->adminlog($this->systemLang['watchMember']);
    }

    function getChannel()
    {
        $channelList = array();
        return $channelList;
        /*
        return array(
            '0'              => '全部渠道',
            self::QD_GENUINE => '正版渠道',
            self::QD_WIFI    => 'Wifi渠道',
            self::QD_BREAK   => '越狱渠道',
            self::QD_NO4     => 'No.4渠道',
        );
        */
    }

    function add(){
        $this->channels = $this->getChannel();
        if( ! empty($_POST) ){
            $add = $_POST['form'];
            //echo "<pre>";
            //print_r($_POST['channels']);
            $add['channels'] = '';
            //exit;
            $add["user_pwd"] = md5($add["user_pwd"]);
                //$games = empty($_POST['games']) ? array(0) : $_POST['games'];
                $user_lm = empty($_POST['user_lm']) ? array(0) : $_POST['user_lm'];
                //根据用户组获取用户游戏权限
                $games = $this->g5_admin_group->getGameBygroup($add['group_id']);
                $add['user_games'] = $games['group_game'];
                $add['user_unions']   = '';
//var_dump($add); exit;
                if($this->g5_admin_user->insert($add)){
                    $this->adminlog($this->systemLang['addManageSuccess'].$add['user_name']);
                    $this->report(true, BASEURI.'user/index');
                }else{
                    $this->report(false, BASEURI.'user/index');
                }
        }else{
            $this->groups       = $this->g5_admin_group->getGroupList();
            $this->formAction   = BASEURI . 'user/add';
            $this->is_validator = TRUE;
            //$this->games = $this->g5_game->allGamesSelecter();
            //$this->unions = $this->g5_unions->getUnionList();
        }
    }

    function edit(){
        $this->channels = $this->getChannel();

        $id = isset($_GET[2]) ? $_GET[2] : 0;
        if( ! empty($_POST) ){
            $user = $_POST['form'];
            if($user['user_pwd'] != "" ){
                $user["user_pwd"] = md5($user['user_pwd']);
            }else{
                unset($user['user_pwd']);
            }

            $user['channels'] = '';

            //$games = empty($_POST['games']) ? array(0) : $_POST['games'];
            $user_lm = empty($_POST['user_lm']) ? array(0) : $_POST['user_lm'];
            $games = $this->g5_admin_group->getGameBygroup($user['group_id']);
            $user['user_games'] = $games['group_game'];
            $user['user_unions']   = '';
            if($this->g5_admin_user->update($user, array('user_id'=>$user['user_id']))){
                $this->adminlog($this->systemLang['updateManageSuccess'].$user['user_name']);
                $this->report($this->systemLang['updateManageSuccess'], BASEURI. 'user/index');
            }else{
                $this->report($this->systemLang['updateManageFailed'], BASEURI. 'user/index');
            }
        }else{
            $group = $this->g5_admin_group->getGroupList();
            $user  = $this->g5_admin_user->getUser($id);
            $this->formAction = BASEURI."user/edit";
            $this->groups     = $group;
            $this->user       = $user;
            //2012-3-31修改
            //$this->userGames = explode(',', $user['user_game']);
            //$group = $this->g5_admin_group->getOne('*', array('group_id'=>$user['group_id']));
            //$this->userGames = explode(',', $group['group_game']);
            //$this->games = $this->g5_game->getGameByGroup($user['group_id']);
            $myunions = empty($this->user['user_union']) ? array(0) : explode(',', $this->user['user_union']);
            /*
            echo "<pre>";
            print_r($this->games);
            print_r($channels);
            exit();
            */
            $this->mychannels = explode(',', $user['channels']);

            //$this->unions = $this->g5_unions->getUnionList();
            $this->myunions = $myunions;
            $this->display('user.add');
        }

    }

    function delete(){
        $ids = empty($_POST['select_id']) ? array($_GET[2]) : $_POST['select_id'];
        if($this->g5_admin_user->in(array('user_id'=>$ids))->delete()){
            $this->adminlog($this->systemLang['delManageSuccess'].implode(',', $ids));
            $this->report($this->systemLang['delManageInfoSuccess'], BASEURI. 'user/index');
        }else{
            $this->report($this->systemLang['delManageInfoSuccess'], BASEURI. 'user/index');
        }

    }

    /*
    *  修改个人密码
    **/
    function editmypwd(){

        if($_POST){
            if($_POST['pwd']['old_pwd']=='' || $_POST['pwd']['pwd2']==''){
				$this->report($this->systemLang['updatePersonPwd'], '/user/editmypwd');
            }

            $user    =    $_SESSION['adminmanager'];

            if($user['user_pwd'] != md5($_POST['pwd']['old_pwd'])){
                $this->report($this->systemLang['oldPwdFail'], '/user/editmypwd');
            }
            $where = array('user_id'=>$user['user_id']);
            if($this->g5_admin_user->update(array('user_pwd'=>md5($_POST['pwd']['pwd2']),'user_last_reset'=>time()),$where)){
                $_SESSION['adminmanager']['user_pwd'] = md5($_POST['pwd']['pwd2']);
                $_SESSION['adminmanager']['user_last_reset'] = time();
                $_SESSION['update_password'] = false;
                unset($_SESSION['update_password']);
            }else{
                 $this->report($this->systemLang['oldPwdFail'],BASEURI.'user/editmypwd');
            }
            $this->adminlog($this->systemLang['updatePersonPwd']);
            $this->report($this->systemLang['updatePwdSuccess'],BASEURI.'user/editmypwd');
        }
    }
}

